<?php

class Captcha {

    var $chars = 'ABCDEFGHKLMNPQRSTUVWYZ123456789'; // O  and 0 (Zero) are visually similar, that's why I am not using it
    var $RandomStr = '';

    function OutputCaptcha($width=80, $height=20, $length=6) {
        for ($i = 0; $i < $length; $i++) { // Generating the captcha string
            $pos = mt_rand(0, strlen($this->chars) - 1);
            $this->RandomStr .= substr($this->chars, $pos, 1);
        }
        $ResultStr = $this->RandomStr;
        $NewImage = imagecreatefromjpeg("bg_captcha2.jpg"); //image create by existing image and as back ground 
        $TextColor = imagecolorallocate($NewImage, 0, 0, 0); //text color-Black
        imagestring($NewImage, 5, 13, 1, $ResultStr, $TextColor); // Draw a random string horizontally 
        $_SESSION['captcha_code'] = strtolower($ResultStr); // carry the data through session
        header("Content-type: image/jpeg"); // out out the image 
        imagejpeg($NewImage); //Output image to browser 
    }

}

?>